package com.tools.commons.enums.convert;

import com.tools.commons.enums.TypeEnum;

import javax.persistence.AttributeConverter;

public abstract class AbstractConverter<E extends TypeEnum<Type>, Type> implements AttributeConverter<E, Type> {

    private final Class<E> clazz;

    protected AbstractConverter(Class<E> clazz) {
        this.clazz = clazz;
    }

    @Override
    public Type convertToDatabaseColumn(E attribute) {
        return attribute == null ? null : attribute.getType();
    }

    @Override
    public E convertToEntityAttribute(Type dbData) {
        return dbData == null ? null : TypeEnum.typeToEnum(clazz, dbData);
    }
}
